package pl.prv.polanie.client.dialogs;

import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.dto.CSSubjectNameDTO;
import pl.prv.polanie.client.model.Cache;
import pl.prv.polanie.client.model.ServiceHolder;
import pl.prv.polanie.client.panels.lists.SubjectNamesList;
import pl.prv.polanie.client.widgets.CustomDialogBox;
import pl.prv.polanie.client.widgets.TextBoxStyled;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @author Janusz Prokulewicz
 * Created: Apr 19, 2008
 */

public class CreateSubjectNameDialog extends CustomDialogBox {

	private static final String STYLE__CREATE_SUBJECT_EVENT_DIALOG = "createSubjectEventDialog";
	
	
	/** Podzial, dla ktorego jest tworzony nowy/edytowany istniejacy subjectEvent.*/
	private SubjectNamesList subjectNamesList = null;
	
	
	
	
	/***/
	private FlexTable widgetsFT = new FlexTable();
	
	private Label subjectNameL= new Label("Subject Name");
	private TextBox subjectNameTB = new TextBoxStyled();

	private Label symbolL= new Label("Symbol");
	private TextBox symbolTB = new TextBoxStyled();

	
//	private Button loadDataB = new Button("load dataa");

	private Button saveOrCreateAndCloseB = new Button("save and close");
	private Button saveOrCreateB = new Button("save/create");
	private Button closeB = new Button("close");
	
	
	//referencja do dto edytowanego sn
	private CSSubjectNameDTO csSubjectNameDTO = null;
	
	
	public CreateSubjectNameDialog(SubjectNamesList SubjectNamesList, CSSubjectNameDTO csSN) {
		this.subjectNamesList = SubjectNamesList;
		this.csSubjectNameDTO = csSN;
		
		layoutWidgets();
		setupWidgets();

		if (csSN != null) {
			setText("Edit subject name");
		} else {
			setText("Create new subject name");
		}

		
		setWidget(widgetsFT);
		//setStyleName(STYLE__CREATE_SUBJECT_EVENT_DIALOG);
		
		
		loadDataToWidgets();
	}



	private void setupWidgets() {
		
		subjectNameTB.setText("");
		subjectNameTB.setWidth("300px");
		
		saveOrCreateB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				boolean closeWindowAfterSaving = false;
				saveCSSubjectName(closeWindowAfterSaving);
			}
		});
		
		saveOrCreateAndCloseB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				boolean closeWindowAfterSaving = true;
				saveCSSubjectName(closeWindowAfterSaving);
			}
		});
		
		closeB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				hide();
			}
		});
		
		
//		loadDataB.addClickListener(new ClickListener() {
//			public void onClick(Widget sender) {
//				loadDataToWidgets();
//			}
//		});
	}



	private void layoutWidgets() {
		widgetsFT.setWidget(0, 0, subjectNameL);
		widgetsFT.setWidget(0, 1, subjectNameTB);

		widgetsFT.setWidget(1, 0, symbolL);
		widgetsFT.setWidget(1, 1, symbolTB);
		
//		widgetsFT.setWidget(2, 0, loadDataB);
		widgetsFT.setWidget(2, 1, saveOrCreateAndCloseB);
		
		widgetsFT.setWidget(3, 0, saveOrCreateB);
		widgetsFT.setWidget(3, 1, closeB);
		
	}
	
	/**
	 * TODO metoda validujaca dane
	 */
	private boolean validateEnteredData() {
		return true;
	}
	

	
	/**
	 * Wczytuje z cache'a dane do kontrolek. W przypadku edycji
	 * istniejacego SE ustawiane sa takze w kontrolkach odpowiednie wartosci.
	 */
	private void loadDataToWidgets() {

		String name = "";
		String symbol = "";
		
		if (csSubjectNameDTO != null) {
			
			Planner.log("edycja subject name");
			name = csSubjectNameDTO.getName();
			symbol = csSubjectNameDTO.getSymbol();
			
		} else {
			Planner.log("tworzenie subject name");
		}

		subjectNameTB.setText("" + name);
		symbolTB.setText("" + symbol);
	}

	/**
	 * Zapisuje/aktualizuje rekord. 
	 * @param closeWindowAfterSaving
	 */
	private void saveCSSubjectName(final boolean closeWindowAfterSaving) {
		if (validateEnteredData()) {
			final CSSubjectNameDTO csSN = new CSSubjectNameDTO();
			
			//w przypadku edycji AT ustawiamy id
			if (csSubjectNameDTO != null) {
				csSN.setId(csSubjectNameDTO.getId());
			}
			
			
			//ustawienie atrybutow w dto
			csSN.setName(subjectNameTB.getText());
			csSN.setSymbol(symbolTB.getText());
			
			
			
			AsyncCallback callback = new AsyncCallback() {
				public void onFailure(Throwable caught) {
					Planner.log("CreateSubjectNameDialog.saveCSSubjectName() FAILURE");
				}
				public void onSuccess(Object result) {
					Long res = (Long)result;
					Planner.log("CreateSubjectNameDialog.saveCSSubjectName() SUCCESS -> zwrocone ID = " + res);

					//update w cache
					if (res.longValue() != 0) {
						csSN.setId(res.longValue());
						Cache.saveOrUpdateCSSubjectName(csSN);
						
						//TODO dodac do kontrolki tabelki
						if (subjectNamesList != null) {
							subjectNamesList.addCSSubjectName(csSN);
						}
					}

					
					if (closeWindowAfterSaving) {
						hide();
					}
				}
			};
			
			ServiceHolder.get().saveCSSubjectName(csSN, callback);
		}
	}
	
	public void setFocus() {
		subjectNameTB.setFocus(true);
		subjectNameTB.setSelectionRange(0, subjectNameTB.getText().length());
	}

}
